/**
 * 获取缓存中用于请求的key
 */
var getStorageKey = function() {
	var storeId = uni.getStorageSync('storeId') || null;
	return storeId;
}
/**
 * 验证是否授权登录过
 * 将当前页面的参数载入缓存,并跳转至授权登录页面
 */
var verifyAuth = function(callback) {
	if (getStorageKey()) {
		callback();
		return;
	}
	uni.showModal({
		title: '授权登陆',
		content: '为了为您提供相关服务，推荐您授权信息后查看',
		success: function(res) {
			if (res.confirm) {
				var pages = getCurrentPages() //获取加载的页面
				var currentPage = pages[pages.length - 1] //获取当前页面的对象
				var url = currentPage.route //当前页面url
				var options = currentPage.options //如果要获取url中所带的参数可以查看options
				var param = '';
				var index = 0;
				for (var key in options) {
					if (index == 0) {
						var and = '?';
					} else {
						var and = '&';
					}
					param += and + key + '=' + options[key];
					++index;
				}
				var path = '/' + url + param;
				uni.setStorage({
					key: 'path',
					data: path,
					success: function() {
						uni.navigateTo({
							url: '/pages/merchant/login/login'
						});
					}
				})
			}
		}
	})
}
/**
 * 分页加载提示
 */
var getPageConfig = function() {
	return {
		current_page: 1,
		loading_msg: '加载中……',
		finish_msg: '已展示全部',
		empty_msg: '什么都没找到',
		show_loading: false,
		show_finish: false,
		show_empty: false,
	}
}
/**
 * get参数转json对象
 */
var urlFormatToJson = function(url) {
	var param = {};
	url.replace(/([^?&]+)=([^?&]+)/g, function(s, v, k) {
		param[v] = decodeURIComponent(k);
		return k + '=' + v;
	});
	return param;
}
var getGetParam = function(url) {
	var arr = url.split('?')[1].split('&')
	var theRequest = new Object();
	for (var i = 0; i < arr.length; i++) {
		var kye = arr[i].split("=")[0]
		var value = arr[i].split("=")[1]
		// 给对象赋值
		theRequest[kye] = value
	}
	return theRequest;
}
/**
 * 订单、送菜每月核销
 */
var scanCode = function() {

	uni.scanCode({
		success: function(res) {
			uni.showLoading();
			var result = res.result;
			var jsonResult = urlFormatToJson(result);
			console.log(jsonResult)
			// return;
			if (!jsonResult.order_type) {
				uni.showToast({
					title: '请扫描正确的核销码~',
					icon: 'none',
					success() {
						setTimeout(function() {
							uni.hideLoading();
						}, 500);
					}
				});
				return;
			}
			switch (Number(jsonResult.order_type)) {
				case 1:
					//免费菜品
					if (!jsonResult.id) {
						uni.showToast({
							title: '请扫描正确的核销码~',
							icon: 'none',
							success() {
								setTimeout(function() {
									uni.hideLoading();
								}, 500);
							}
						});
						break;
					}
					uni.hideLoading();
					uni.navigateTo({
						url: "/pages/merchant/cancellation/cancellationMonth?" + result
					});
					break;
				case 2:
					//订单
					if (!jsonResult.order_id) {
						uni.showToast({
							title: '请扫描正确的核销码~',
							icon: 'none',
							success() {
								setTimeout(function() {
									uni.hideLoading();
								}, 500);
							}
						});
						break;
					}
					uni.hideLoading();
					uni.navigateTo({
						url: "/pages/merchant/cancellation/cancellation?" + result
					});
					break;
				default:
					uni.showToast({
						title: '请扫描正确的核销码~',
						icon: 'none',
						success() {
							setTimeout(function() {
								uni.hideLoading();
							}, 500);
						}
					});
					break;
			}

		}
	});
}
export default {
	getStorageKey,
	verifyAuth,
	getPageConfig,
	urlFormatToJson,
	scanCode,
	getGetParam
}
